local jili = fk.CreateSkill {

  name = "joy__bjili",

  tags = { Skill.Compulsory, },

}



jili:addEffect(fk.TargetConfirming, {
  name = "joy__bjili",
  mute = true,
  frequency = Skill.Compulsory,
  events = {fk.TargetConfirming,fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    if event == fk.CardUseFinished then
      return player:hasSkill(jili.name) and data.card and data.damageDealt and data.extra_data and data.extra_data.joy__bjili and table.contains(data.extra_data.joy__bjili,player.id)
    else
      return player:hasSkill(jili.name) and not target.dead and target:distanceTo(player) == 1 and
      data.card.color == Card.Red and (data.card.type == Card.TypeBasic or data.card:isCommonTrick()) and
      data.from ~= player.id and not table.contains(data:getAllTargets(), player.id) and
      U.canTransferTarget(player, data)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardUseFinished then
      player:drawCards(1,jili.name)
    else
      if data.card.is_damage_card or table.contains({"dismantlement", "snatch", "chasing_near"}, data.card.name) or data.card.is_derived then
        player:broadcastSkillInvoke(jili.name, 1)
        room:notifySkillInvoked(player, jili.name, "negative")
      else
        player:broadcastSkillInvoke(jili.name, 2)
        room:notifySkillInvoked(player, jili.name, "control")
      end
      room:doIndicate(player.id, {target.id})
      local targets = {player.id}
      if type(data.subTargets) == "table" then
        table.insertTable(targets, data.subTargets)
      end
      data:addTarget(targets)
      data.extra_data = data.extra_data or {}
      data.extra_data.joy__bjili = data.extra_data.joy__bjili or {}
      table.insertIfNeed(data.extra_data.joy__bjili,player.id)
    end
  end,
})
jili:addEffect(fk.CardUseFinished, {
  name = "joy__bjili",
  mute = true,
  frequency = Skill.Compulsory,
  events = {fk.TargetConfirming,fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    if event == fk.CardUseFinished then
      return player:hasSkill(jili.name) and data.card and data.damageDealt and data.extra_data and data.extra_data.joy__bjili and table.contains(data.extra_data.joy__bjili,player.id)
    else
      return player:hasSkill(jili.name) and not target.dead and target:distanceTo(player) == 1 and
      data.card.color == Card.Red and (data.card.type == Card.TypeBasic or data.card:isCommonTrick()) and
      data.from ~= player.id and not table.contains(data:getAllTargets(), player.id) and
      U.canTransferTarget(player, data)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardUseFinished then
      player:drawCards(1,jili.name)
    else
      if data.card.is_damage_card or table.contains({"dismantlement", "snatch", "chasing_near"}, data.card.name) or data.card.is_derived then
        player:broadcastSkillInvoke(jili.name, 1)
        room:notifySkillInvoked(player, jili.name, "negative")
      else
        player:broadcastSkillInvoke(jili.name, 2)
        room:notifySkillInvoked(player, jili.name, "control")
      end
      room:doIndicate(player.id, {target.id})
      local targets = {player.id}
      if type(data.subTargets) == "table" then
        table.insertTable(targets, data.subTargets)
      end
      data:addTarget(targets)
      data.extra_data = data.extra_data or {}
      data.extra_data.joy__bjili = data.extra_data.joy__bjili or {}
      table.insertIfNeed(data.extra_data.joy__bjili,player.id)
    end
  end,
})

return jili